package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @author ：Shijiu
 * @create  2019/09/08 20:20
 * @description ：权限的service
 * @version: 1.0
 */

@Service
@Transactional
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;

    /**
     * 分页查询权限
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Permission> page=permissionDao.findByPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page);
    }

    /**
     * 添加权限
     * @param permission
     */
    @Override
    public void add(Permission permission) {
        permissionDao.add(permission);
    }

    /**
     * 根据id查询权限
     * @param id
     * @return
     */
    @Override
    public Permission findPermissionById(Integer id) {
        return permissionDao.findPermissionById(id);
    }

    @Override
    public void update(Permission permission) {
        permissionDao.update(permission);
    }

    @Override
    public void delete(Integer id) {
        //先查询是否有角色关联其权限
        long count=permissionDao.findRoleById(id);
        if (count!=0){
            //有角色与其关联，不能删除
            throw new RuntimeException("该权限有角色正在使用，删除失败");
        }else {
            //直接删除
            permissionDao.delete(id);
        }

    }
    //查询所有权限
    @Override
    public List<Permission> findAllPermission() {
        //查询权限列表
        return permissionDao.findAllPermission();
    }
}
